<%-- 
    Document   : LoginPage
    Created on : Nov 20, 2012, 4:57:52 PM
    Author     : vupttse
--%>

<%@page import="ass.data.UserEntity"%>
<%@page import="ass.data.UserDAL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link href="css/modern.css" rel="stylesheet">
        <link href="css/modern-responsive.css" rel="stylesheet">
        <link href="css/site.css" rel="stylesheet" type="text/css">

        <script src="js/jquery-1.8.2.min.js"></script>
        <script src="js/google-analytics.js"></script>
        <script src="js/github.info.js"></script>
        <script src="js/sharrre/jquery.sharrre-1.3.4.min.js"></script>
        <script src="js/carousel.js"></script>
        <script src="js/dropdown.js"></script>
        <script src="js/slider.js"></script>
        <script src="js/hover.js"></script>

        <script>
            //            Validate the input of the form when submit
            function validateForm()
            {
                var x=document.forms["LoginForm"]["username"].value;
                var y=document.forms["LoginForm"]["password"].value;
                if (x==null || x=="" || y==null || y=="")
                {
                    alert("Please enter username and password!");
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <%

        %>
        <div class="page">
            <jsp:include page="Navigation.jsp"></jsp:include>

                <div class="page-region bg-color-red fg-color-white">
                    <div class="page-region-content" >   
                        <div class="grid"><div class="row">
                                <div class="span5" style="margin-left: 20px">
                                    <h1 class="fg-color-white" style="margin-left: 20px;">Login Page</h1>
                                    <table>
                                        <form name="LoginForm" action="" method="post" onsubmit="return validateForm()">
                                            <tr>
                                                <td>Username: </td>
                                                <td><input type="text" name="username" value=""></td>
                                            </tr>
                                            <tr>
                                                <td>Password: </td>
                                                <td><input type="password" name="password" value=""></td>
                                            </tr>                                  
                                            <tr>
                                                <td></td>
                                                <td>
                                                    <input type="submit" value="Log in">
                                                    <input type="reset" value="Reset">
                                                </td>
                                            </tr>
                                        </form>
                                        <tr><td colspan="2">
                                                You don't have an account? You want to have more discount?
                                                <a href="Registration.jsp"><i class="icon-link fg-color-white"></i>Click here</a> to join us now!
                                            </td></tr>
                                        <%
                                            //Get username and password from request, check whether correct or not
                                            String username = request.getParameter("username");
                                            String password = request.getParameter("password");
                                            if (username != null && password != null)
                                                try {
                                                    if (UserDAL.checkLogin(username, password)) {
                                                        UserEntity user = UserDAL.getUser(username);
                                                        session.setAttribute("currentUser", user);
                                                        if (user.getRole().trim().equals("Customer")) {
                                                            response.sendRedirect("ViewProduct.jsp");
                                                        } else if (user.getRole().trim().equals("Manager")) {
                                                            response.sendRedirect("ManagerPages/MainManagerPage.jsp");
                                                        } else {
                                                            response.sendRedirect("UnderConstruction.jsp");
                                                        }
                                                    } else {%>
                                    <tr>
                                        <td colspan="2">
                                            <em style="color: white">Login fail! Please try again!</em></br>
                                            <a href="UnderConstruction.jsp"><i class="icon-link fg-color-white"></i>Forgot your password?</a>
                                        </td>
                                    </tr>

                                    <% }
                                        } catch (Exception e) {
                                        }
                                    %>  
                                </table>

                            </div>

                            <div class="span6 bg-color-red" style="height: 340px; margin-left: 50px">
                                <h2 class="fg-color-white">Hot Available!</h2>
                                <div class="carousel" data-role="carousel" style="height: 280px;" data-param-duration="150">
                                    <div class="slides border-color-darken">
                                        <div class="slide" id="slide1">
                                            <a href="UnderConstruction.jsp"><img src="images/products/p1.jpg" width="460px" height="280px"> </a>
                                        </div>
                                        <div class="slide" id="slide2">   
                                            <a href="UnderConstruction.jsp"><img src="images/products/p2.jpg" width="460px" height="280px"> </a>
                                        </div>
                                        <div class="slide" id="slide3">
                                            <a href="UnderConstruction.jsp"><img src="images/products/p3.jpg" width="460px" height="280px"> </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <hr>
                <%@include file="footer.html" %>
            </div>
        </div>
    </body>
</html>
